{    
Indicator:          _SharpeRatioQ     (Quick simple version)

Description:        Divides a time period into segments, and calculates price gain in percent for each segment.
                    If logarithmic growth is occuring, each segment will have a similar percentage gains.

Calculation load:   With Offset=0, calculations include LastBarOnChart which is always changing with each tick.
                    Use this to ensure most recent data is included in analysis.

                    With Offset=1, calculations do NOT include LastBarOnChart.
                    This eliminates the constant calculations that are triggered while Close of current bar 
                    continuously updates with each tick throughout the day.
                    Use this setting to minimize calculation load while monitoring stocks during the day,

Author:             Mark J. Krisburg 

Last Modified:      06/04/07
                    01/09/08        Add RSSum = Mult1*SR1 + Mult2*SR2
                    01/21/08        Add 4th SR (SR4) and Summation output
                    08/18/08        improve consistency between chart and RS versions
                    01/27/10        clean code
                    01/30/10        post to forum
} 


Inputs:        
    Price(TypicalPrice), 
    SegLength(1),                    // use = 1 for weekly or monthly monthly charts, = 5 for daily charts (1 week) or = 22 (1 month)
    SegsPerYear(251),                // = 52 if SegLength = 5 on Day Chart(1 week), = 12 if SegLength = 1 on Monthly Chart 
    Segments1(50),                   // number of segments used to calculate SR1
    Segments2(0),                    // number of segments used to calculate SR2
    Segments3(0),                    // number of segments used to calculate SR3
    Segments4(0),                    // number of segments used to calculate SR4

    Offset1(0),
    Offset2(0),
    Offset3(0),
    Offset4(0),

    RiskFreeReturn(2),               // annualized risk free return in percent.
    CalcSeconds(60),                 // Recalculate SR every 'CalcTicks' tick.
                                     // cause a recalculation (one day delay does not change result significantly).
    Ref1(0.00001),                   // if non-zero, plot value
    Ref2(0),
    Ref3(0);
    
Vars:                                // for _SharpeRatio
    DoCalc(true),
    x(0),                            // dummy variable
    
    avgRR1(0),                       // average return rate for 'Segments1' segments
    sdRR1(0),                        // standard deviation of return rates
    intrabarpersist annRR1(0),       // annualized rate of return = RR * 12 (1 segment = 1 month)
    annSD1(0),                       // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month)
    excessRR1(0),                    // excess return = annualized rate of return - risk free return
    intrabarpersist SR1(0),          // Sharpe Ratio = Excess return / annualized standard deviation of returns
    intrabarpersist AbsSR1(0),       // absolute value of Sharpe Ratio (Not currently in use)
    

    avgRR2(0),                       // average return rate for 'SegMents2' segments
    sdRR2(0),                        // standard deviation of return rates
    intrabarpersist annRR2(0),       // annualized rate of return = RR * 12 (1 segment = 1 month)
    annSD2(0),                       // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month)
    excessRR2(0),                    // excess return = annualized rate of return - risk free return
    intrabarpersist SR2(0),          // Sharpe Ratio = Excess return / annualized standard deviation of returns
    intrabarpersist AbsSR2(0),       // absolute value of Sharpe Ratio

    avgRR3(0),                       // average return rate for 'SegMents3' segments
    sdRR3(0),                        // standard deviation of return rates
    intrabarpersist annRR3(0),       // annualized rate of return = RR * 12 (1 segment = 1 month)
    annSD3(0),                       // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month)
    excessRR3(0),                    // excess return = annualized rate of return - risk free return
    intrabarpersist SR3(0),          // Sharpe Ratio = Excess return / annualized standard deviation of returns
    intrabarpersist AbsSR3(0),       // absolute value of Sharpe Ratio

    avgRR4(0),                       // average return rate for 'SegMents3' segments
    sdRR4(0),                        // standard deviation of return rates
    intrabarpersist annRR4(0),       // annualized rate of return = RR * 12 (1 segment = 1 month)
    annSD4(0),                       // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month)
    excessRR4(0),                    // excess return = annualized rate of return - risk free return
    intrabarpersist SR4(0),          // Sharpe Ratio = Excess return / annualized standard deviation of returns
    intrabarpersist AbsSR4(0);       // absolute value of Sharpe Ratio

            
    
DoCalc = _TickSeconds(CalcSeconds)> 0;    

if DoCalc then begin

    if Segments1 > 0 then begin
        x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments1, RiskFreeReturn, 
            Offset1, avgRR1, sdRR1, annRR1, annSD1, excessRR1, SR1);
        Plot1(SR1, "SR1"); 
    end;
                 
    if Segments2 > 0 then begin 
        x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments2, RiskFreeReturn, 
            Offset2, avgRR2, sdRR2, annRR2, annSD2, excessRR2, SR2);
        Plot2(SR2, "SR2");
    end;         
    
    if Segments3 > 0 then begin
        x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments3, RiskFreeReturn, 
            Offset3, avgRR3, sdRR3, annRR3, annSD3, excessRR3, SR3);
        Plot3(SR3, "SR3"); 
     end; 
             
    if Segments4 > 0 then begin
        x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments4, RiskFreeReturn, 
            Offset4, avgRR4, sdRR4, annRR4, annSD4, excessRR4, SR4);
        Plot4(SR4, "SR4"); 
    end;         

end;

                                                                            
if Ref1 <> 0 then Plot31(Ref1, "Ref1");
if Ref2 <> 0 then Plot32(Ref2, "Ref2");
if Ref3 <> 0 then Plot33(Ref3, "Ref3");
